package com.gph.saviorframework.mail.service.impl;

import com.cloopen.rest.sdk.CCPRestSDK;
import com.cloopen.rest.sdk.utils.PropertiesUtil;
import com.gph.saviorframework.mail.service.SmsMessageSendService;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Properties;
import java.util.Set;

/**
 * Created by dell on 2017/5/11.
 */
@Component
public class SmsMessageSendServiceImpl implements SmsMessageSendService {
    /**
     *
     * @param mobile 电话
     * @param modeId  模版Id
     * @param params   参数
     * @return
     */
    public String sendMessage(String mobile, String modeId, String... params) {
        HashMap<String, Object> result = null;
        Properties properties=new Properties();
        InputStream inputStream=getClass().getResourceAsStream("/sms.properties");
        try {
            properties.load(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        CCPRestSDK restAPI = new CCPRestSDK();
        restAPI.init(properties.get("ip").toString(), properties.get("port").toString());// 初始化服务器地址和端口，格式如下，服务器地址不需要写https://
        restAPI.setAccount(properties.get("account_sid").toString(), properties.get("auth_token").toString());// 初始化主帐号和主帐号TOKEN
        restAPI.setAppId(properties.get("app_id").toString());// 初始化应用ID
        result = restAPI.sendTemplateSMS(mobile,modeId,params);

        System.out.println("SDKTestSendTemplateSMS result=" + result);

        if ("000000".equals(result.get("statusCode"))) {
            //正常返回输出data包体信息（map）
            HashMap<String, Object> data = (HashMap<String, Object>) result.get("data");
            Set<String> keySet = data.keySet();
            for (String key : keySet) {
                Object object = data.get(key);
                System.out.println(key + " = " + object);
            }
        } else {
            //异常返回输出错误码和错误信息
            System.out.println("错误码=" + result.get("statusCode") + " 错误信息= " + result.get("statusMsg"));
        }
        return null==result.get("statusCode")?"-1":result.get("statusCode").toString();
    }
}
